//
//  HBListTableView.m
//  FMSManager
//
//  Created by hongbao.cui on 15-1-8.
//  Copyright (c) 2015年 hongbao.cui. All rights reserved.
//

#import "HBListTableView.h"
#import "HBTableViewCell.h"
#import "HBTableViewHeaderView.h"
#import "HBTableViewFooterView.h"
#import "ISMutileButton.h"
#import "HBSimpleTableViewCell.h"
#import "LookHBSimpleTableViewCell.h"
#import "Sqlite.h"
@interface HBListTableView()<UITableViewDataSource,UITableViewDelegate,HBTableViewHeaderViewDelegate,HBTableViewFooterViewDelegate,WebViewCellDelegate>{
    BOOL isSingle;
    ISMutileButton *assignBtn;
    NSString *myAnswer;
    NSDictionary *dataDict;
    NSString *collection_finish_flag;
    NSLock *lock;
}
@property(nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation HBListTableView
@synthesize hbdelegate;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)dealloc{
    if (_loadedCells) {
        _loadedCells = nil;
    }
    if(self.dataArray){
        [self.dataArray removeAllObjects];
        self.dataArray = nil;
    }
    if (self.pngPath) {
        self.pngPath = nil;
    }
    if (self.listViewBlock) {
        self.listViewBlock = nil;
    }
    if (self.listInputBlock) {
        self.listViewBlock = nil;
    }
    if (self.listDraftBlock) {
        self.listDraftBlock = nil;
    }
    if (self.listPhotoBlock) {
        self.listPhotoBlock = nil;
    }
    if (hbdelegate) {
        hbdelegate = nil;
    }
    if (self.pngPath) {
        self.pngPath = nil;
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGFloat offset = keyboardRect.size.height;
    CGRect frame = self.frame;
    CGFloat height = self.contentSize.height;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    if (height-frame.size.height>0) {
        [self setContentOffset:CGPointMake(0, heightoffset)];
        [self setContentSize:CGSizeMake(self.contentSize.width,offset+heightoffset-frame.size.height)];
    }
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//    [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGFloat offset = keyboardRect.size.height;
    CGRect frame = self.frame;
    frame.size.height += keyboardRect.size.height;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
//    self.frame = frame;
    [self setContentSize:CGSizeMake(self.contentSize.width,contentSize)];
    [UIView commitAnimations];
}
-(id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:UITableViewStylePlain];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        UIView *aView = [[UIView alloc] init];
        aView.backgroundColor = [UIColor whiteColor];
        self.backgroundView = aView;
        if (!self.dataArray) {
            self.dataArray = [NSMutableArray arrayWithCapacity:1];
        }
        lock = [[NSLock alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}
-(void)setModelViewData:(NSDictionary *)dict WithQuestionCount:(NSInteger)count{
    HBTableViewHeaderView *headerView = [[HBTableViewHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    headerView.tag = 99;
    headerView.simpleBlock = ^(HBTableViewHeaderView *headerView){
        if (self.listViewBlock) {
            self.listViewBlock(self,headerView);
        }
    };
    [headerView setDelegate:self];
    [headerView setDataReload:dict WithQuestionCount:count];
    self.tableHeaderView = headerView;
    NSArray *dataArray = [dict objectForKey:@"options"];
    if (_loadedCells) {
        [_loadedCells removeAllObjects];
    }
    if (self.dataArray) {
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:dataArray];
    }
    //collection_finish_flag  作业管理种收藏的标识、
    collection_finish_flag = [dict objectForKey:@"collection_finish_flag"];
    NSString *qtype = [dict objectForKey:@"qtype"];
//    _rowCount = [self.dataArray count];
    if ([qtype isEqualToString:@"1"]||[qtype isEqualToString:@"3"]) {// 2多选题
        isSingle = YES;
    }else{//2多选选//移动学习4是简单题
        isSingle = NO;
    }
    if (_tableType == HBListTableViewTypeTest) {
        myAnswer = [dict objectForKey:@"user_answer"];
    }
    if (_tableType == HBListTableViewTypeResultLook) {
        myAnswer = [dict objectForKey:@"answer"];
        HBTableViewFooterView *footerView = [[HBTableViewFooterView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        [footerView setDelegate:self];
        [footerView setDataReload:dict];
        self.tableFooterView = footerView;
    }
    if (_tableType == HBListTableViewTypeResultLookNoAnswer) {
        myAnswer = [dict objectForKey:@"user_answer"];//选择的答案
    }
    dataDict = dict;
    [self reloadData];
}
#pragma mark - Table view data source

#pragma mark - section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSString *qtype = [dataDict objectForKey:@"qtype"];
    NSArray *has_sub_array = [dataDict objectForKey:@"has_sub_Array"];
    FMSCHBLog(@"qtype-----%@",qtype);
    if (([qtype isEqualToString:@"0"])&&(_tableType==HBListTableViewTypeResultLook)) {
        return 0;
    }else if([qtype isEqualToString:@"1"]||[qtype isEqualToString:@"2"]||[qtype isEqualToString:@"3"]){
        return 1;
    }
    else if([qtype isEqualToString:@"4"]){
        return has_sub_array.count + 1;
    }
    return 0;
}

#pragma mark - 行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *qtype = [dataDict objectForKey:@"qtype"];

    if ([qtype isEqualToString:@"0"]&&_isShowSimple==NO) {//0是简单题
        if (IS_STUDY) {
            return [self.dataArray count]+1;
        }else{
            return [self.dataArray count];
        }
    }else if ([qtype isEqualToString:@"1"]||[qtype isEqualToString:@"2"]||[qtype isEqualToString:@"3"]){
        return [self.dataArray count];
    }
    else if ([qtype isEqualToString:@"4"]) {
        NSArray *has_sub_array = dataDict[@"has_sub_Array"];
        if (has_sub_array.count > 0 && section < has_sub_array.count) {
            NSArray *options = has_sub_array[section][@"optionArray"];
            return options.count;
        }
        return 1;
    }
    return 0;
}

#pragma mark - section height
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    NSString *qtype = [dataDict objectForKey:@"qtype"];
    NSArray *has_sub_array = [dataDict objectForKey:@"has_sub_Array"];
    if ([qtype isEqualToString:@"0"]||[qtype isEqualToString:@"1"]||[qtype isEqualToString:@"2"]||[qtype isEqualToString:@"3"]) {
        return 0;
    }
    else if([qtype isEqualToString:@"4"] && has_sub_array.count == 0){
        return 0;
    }
    else if([qtype isEqualToString:@"4"] && has_sub_array.count > 0){
        if (section == has_sub_array.count) {
            return 0;
        }
    }
//    return 44;
    NSString *hKey = [NSString stringWithFormat:@"height_%ld",section];
    CGFloat height = [[_loadedHeaders objectForKey:hKey] floatValue];
    FMSCHBLog(@"-------height--------%f------",height);
    return MAX(height, 1);
}

#pragma mark - header view
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    NSArray *has_sub_array = [dataDict objectForKey:@"has_sub_Array"];
    if (has_sub_array > 0 && section == has_sub_array.count) {
        return nil;
    }
    NSString *key = [NSString stringWithFormat:@"header_%ld",(long)section];
    HBTableViewHeaderView *headerView = _loadedHeaders[key];
    if (!headerView) {
        headerView = [[HBTableViewHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
        headerView.tag = 100;
        [headerView setDelegate:self];
        headerView.isHeaderView = true;
        headerView.sectionIndex = section;
        NSDictionary *has_sub_Dict = has_sub_array[section];
        NSLog(@"子题目: %@",has_sub_Dict[@"content"]);
        
        [headerView setDataReload:has_sub_Dict WithQuestionCount:8];
    }
    return headerView;
}
-(HBTableViewCell *)tableView:(UITableView *)tableView hbTableViewCellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *indentify = [NSString stringWithFormat:@"%ld_%ld",(long)indexPath.section,(long)indexPath.row];
    HBTableViewCell *cell = _loadedCells[indentify];
    if ([self.dataArray count]>0&&indexPath.row<[self.dataArray count]) {
        NSString *html = [self.dataArray objectAtIndex:indexPath.row];
        if (cell == nil) {
            cell = [[HBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"DataDict:dataDict] ;
            cell.tagIndentify = indentify;
            [cell setDelegate:self];
            cell.isNeedB = isSingle;
            NSString *path = [NSString stringWithFormat:@"%@/image",FMPAPERSAVE_PATH];
            NSURL  *url = [NSURL fileURLWithPath:path];
            [cell setHtml:html AndBaseURL:url];
        }
        if (_tableType == HBListTableViewTypeResultLook||_tableType == HBListTableViewTypeResultLookNoAnswer) {
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
    }
    return cell;
}
//简单题后面有不定向选择题
-(HBTableViewCell *)tableView:(UITableView *)tableView hbTableViewSimpleSingleCellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentify = [NSString stringWithFormat:@"%ld_%ld",(long)indexPath.section,(long)indexPath.row];
    HBTableViewCell *cell = _loadedCells[indentify];
    
    NSDictionary *has_sub_Dict = dataDict[@"has_sub_Array"][indexPath.section-1];
    NSArray *has_sub_array = [has_sub_Dict objectForKey:@"optionArray"];
    
    if ([has_sub_array count]>0&&indexPath.row<[has_sub_array count]) {
        NSString *html = [has_sub_array objectAtIndex:indexPath.row];
        if (cell == nil) {
            cell = [[HBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"DataDict:dataDict] ;
            cell.tagIndentify = indentify;
            [cell setDelegate:self];
            cell.isNeedB = isSingle;
            NSString *path = [NSString stringWithFormat:@"%@/image",FMPAPERSAVE_PATH];
            NSURL  *url = [NSURL fileURLWithPath:path];
            [cell setHtml:html AndBaseURL:url];
        }
        if (_tableType == HBListTableViewTypeResultLook||_tableType == HBListTableViewTypeResultLookNoAnswer) {
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
    }
    return cell;
}
-(HBSimpleTableViewCell *)tableView:(UITableView *)tableView hbSimpleTableViewCellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *dequeue = @"myCell";
    HBSimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeue];
    if (cell==nil) {
        cell = [[HBSimpleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:dequeue];
    }
    cell.dataInfoDict = dataDict;
    NSString *user_answer = [dataDict objectForKey:@"user_answer"];
    NSString *userAnswerImage = [dataDict objectForKey:@"userAnswerImage"];
    NSString *paper_id = [dataDict objectForKey:@"paper_id"];
    NSString *index = [dataDict objectForKey:@"index"];
    if ([userAnswerImage isEqualToString:@"0"]||[userAnswerImage isEqualToString:@""]||[userAnswerImage isEqualToString:@"(null)"]) {
        [cell setBtnProperty:cell.textBtn];
        [cell initInputView];
    }else if ([userAnswerImage isEqualToString:@"1"]){
        [cell setBtnProperty:cell.draftBtn];
        [cell initDraftView];
    }else if ([userAnswerImage isEqualToString:@"2"]){
        [cell setBtnProperty:cell.photoBtn];
        [cell initPhotoView];
    }
    if(user_answer.length>0){//&&[userAnswerImage isEqualToString:@"0"]){
        cell.inputTextView.text = user_answer;
        cell.placeholderLabel.hidden = YES;
    }else{
        cell.inputTextView.text = @"";
        cell.placeholderLabel.hidden = NO;
    }
    if (user_answer.length>0&&[userAnswerImage isEqualToString:@"1"]) {
        NSString *strPath = [NSString stringWithFormat:@"%@/image/%@",paper_id,user_answer];
        NSString *path = [FMPAPERSAVE_PATH stringByAppendingPathComponent:strPath];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        UIImageView *iconImageView = (UIImageView *)[cell.draftImageView viewWithTag:100];
        iconImageView.image = image;
    }else{//没用
//        UIImageView *iconImageView = (UIImageView *)[cell.draftImageView viewWithTag:100];
//        UIImage *draftbgImage = [[UIImage imageNamed:@"Exercises-bj.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 40, 10, 10)];
//        iconImageView.image = draftbgImage;
//        [cell initInputView];
    }
    if (user_answer.length>0&&[userAnswerImage isEqualToString:@"2"]) {
        NSString *strPath = [NSString stringWithFormat:@"%@/image/%@",paper_id,user_answer];
        NSString *path = [FMPAPERSAVE_PATH stringByAppendingPathComponent:strPath];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        UIImageView *iconImageView = (UIImageView *)[cell.photoImageView viewWithTag:100];
        iconImageView.image = image;
    }else{//没用
//        UIImageView *iconImageView = (UIImageView *)[cell.photoImageView viewWithTag:100];
//        UIImage *photobgImage = [[UIImage imageNamed:@"Exercises-bj.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 40, 10, 10)];
//        iconImageView.image = photobgImage;
//        [cell initInputView];
    }
    
    __block  HBListTableView *object = self;
    cell.textViewBlock = ^(UITextView *textView){
//        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//        [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    };
    cell.InputBlock = ^(HBSimpleTableViewCell *cell,NSString *text){//文字
        if (self.listInputBlock) {
            self.listInputBlock(object,text);
        }
    };
    cell.DraftBlock =^(UIButton *sender){//草稿
        if (self.listDraftBlock) {
            self.listDraftBlock(object,sender);
        }
    };
    cell.PhotoBlock =^(UIButton *sender){//照相
        if (self.listPhotoBlock) {
            self.listPhotoBlock(object,sender);
        }
    };
    return cell;
}
-(LookHBSimpleTableViewCell *)tableView:(UITableView *)tableView hbNoChangeSimpleTableViewCellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *dequeue = @"myLookCell";
    LookHBSimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeue];
    if (cell==nil) {
        cell = [[LookHBSimpleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:dequeue];
    }
    [cell setReloadData:dataDict];
    return cell;
}

#pragma mark - cell复用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *qtype = [dataDict objectForKey:@"qtype"];
    NSArray *has_sub_array = dataDict[@"has_sub_Array"];
    NSArray *options = [NSArray new];

    NSInteger section = indexPath.section;
    
    if ([qtype isEqualToString:@"4"]) {
        if (has_sub_array.count>0 && section < has_sub_array.count) {
            options = has_sub_array[indexPath.section][@"optionArray"];
            if (options.count > 0) {
                return  [self tableView:tableView hbTableViewSimpleSingleCellForRowAtIndexPath:indexPath];
            }
        }
        return [self tableView:tableView hbSimpleTableViewCellForRowAtIndexPath:indexPath];
    }
    else if ([qtype isEqualToString:@"0"]&&(_tableType!=HBListTableViewTypeResultLook)) {//0是简单题
        if ([self.dataArray count]>0&&([self.dataArray count]-1==indexPath.row)) {
            HBTableViewCell *cell = [self tableView:tableView hbTableViewCellForRowAtIndexPath:indexPath];
            return cell;
        }else{
            if (_tableType == HBListTableViewTypeResultLookNoAnswer) {//返回只显示简单题答案的cell
                return  [self tableView:tableView hbNoChangeSimpleTableViewCellForRowAtIndexPath:indexPath];
            }
            return [self tableView:tableView hbSimpleTableViewCellForRowAtIndexPath:indexPath];
        }
    }
    else if([qtype isEqualToString:@"1"]||[qtype isEqualToString:@"3"]||[qtype isEqualToString:@"2"]){
        return  [self tableView:tableView hbTableViewCellForRowAtIndexPath:indexPath];
    }
    else{
        return  [self tableView:tableView hbTableViewSimpleSingleCellForRowAtIndexPath:indexPath];
    }
}
-(void)tableView:(HBListTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.dataArray count]>0) {
        if (_tableType == HBListTableViewTypeResultLook||_tableType == HBListTableViewTypeResultLookNoAnswer) {
            return;
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSString *html = [self.dataArray objectAtIndex:indexPath.row];
        NSString *selectionStr = [[html componentsSeparatedByString:@"."] objectAtIndex:0];
        
#pragma mark 禁止点击翻页
        if ([hbdelegate respondsToSelector:@selector(HBListTableView:didSelectRowAtIndexPath:userAnswer:)]) {
            [hbdelegate HBListTableView:tableView didSelectRowAtIndexPath:indexPath userAnswer:selectionStr];
        }
        //显示
        NSString *indentify = [NSString stringWithFormat:@"%ld_%ld",(long)indexPath.section,(long)indexPath.row];
        HBTableViewCell *cell = _loadedCells[indentify];
        if (cell.isNeedB) {
            [assignBtn setSelected:NO];
            [cell.xinghao_btn setSelected:YES];
            assignBtn = cell.xinghao_btn;
            if (_tableType == HBListTableViewTypeExercise) {
                if (IS_STUDY) {
                    if (![collection_finish_flag isEqualToString:@"3"]) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"作业未提交,不能查看答案。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alert show];
                        return;
                    }
                }
                HBTableViewFooterView *footerView = [[HBTableViewFooterView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
                [footerView setDelegate:self];
                [footerView setDataReload:dataDict];
                self.tableFooterView = footerView;
            }else{
                
            }
        }else{
            [cell.xinghao_btn setSelected:!cell.xinghao_btn.selected];
        }
    }
    if ([[dataDict allKeys] containsObject:@"has_sub_Array"]) {
        if (_tableType == HBListTableViewTypeResultLook) {
            return;
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSDictionary *has_sub_Dict = [[dataDict objectForKey:@"has_sub_Array"] objectAtIndex:indexPath.section];
        NSArray *has_sub_array = [has_sub_Dict objectForKey:@"optionArray"];
        NSString *html = [has_sub_array objectAtIndex:indexPath.row];
        NSString *selectionStr = [[html componentsSeparatedByString:@"."] objectAtIndex:0];
        if ([hbdelegate respondsToSelector:@selector(HBListTableView:didSelectRowAtIndexPath:userAnswer:WithDict:)]) {
            [hbdelegate HBListTableView:tableView didSelectRowAtIndexPath:indexPath userAnswer:selectionStr WithDict:has_sub_Dict];
        }
        NSString *indentify = [NSString stringWithFormat:@"%ld_%ld",(long)indexPath.section,(long)indexPath.row];
        HBTableViewCell *cell = _loadedCells[indentify];
        if (cell.isNeedB) {//单选暂不支持
            
        }else{//多选
             [cell.xinghao_btn setSelected:!cell.xinghao_btn.selected];
        }
    }
}
- (void)tableCellDidLoadContent:(HBTableViewCell *)cell{
    
    if (!_loadedCells) {
        _loadedCells = [[NSMutableDictionary alloc] init];
    }
    [_loadedCells setObject:cell forKey:cell.tagIndentify];
    //only refresh sizes of rows
    [self beginUpdates];
    [self endUpdates];
//    [self setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
//    NSIndexPath *indexPath = [self indexPathForCell:cell];
//    [self reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}
-(void)tableCellDidRoloadContent:(id)cell{
    
}

#pragma mark - cell高度
-(CGFloat)heightSimpleSingleForCell:(UITableView *)tableView  indexPath:(NSIndexPath *)indexPath{
    UITableViewCell *tbcell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    if ([tbcell isMemberOfClass:[HBSimpleTableViewCell class]]) {
        return FMSCREENHEIGHT/3;
    }
    
    HBTableViewCell *cell = (HBTableViewCell *)tbcell;
    NSArray *has_sub_array = [dataDict objectForKey:@"has_sub_Array"];
    NSDictionary *has_sub_Dict = [NSDictionary new];
    if (has_sub_array && has_sub_array.count > indexPath.section) {
        has_sub_Dict = has_sub_array[indexPath.section];
    }
    NSArray *subOptions = [has_sub_Dict objectForKey:@"optionArray"];
    NSString *html = @"."; //选项
    if (subOptions.count > indexPath.row) {
        html  = subOptions[indexPath.row];
    }
    
//    NSString *user_Answer = [has_sub_Dict objectForKey:@"user_answer"];
    NSString *answer = [has_sub_Dict objectForKey:@"answer"];
    NSString *selectionStr = [[html componentsSeparatedByString:@"."] objectAtIndex:0];
    
    switch (_tableType) {
        case HBListTableViewTypeTest:
            break;
        case HBListTableViewTypeResultLookNoAnswer:
        case HBListTableViewTypeResultLook:
        {
            if (cell.isNeedB) {
                if ([selectionStr isEqualToString:myAnswer]) {
                    assignBtn = cell.xinghao_btn;
                    [cell.xinghao_btn setSelected:YES];
                }else{
                    [cell.xinghao_btn setSelected:NO];
                }
            }else{
                if(answer.length>0){
                    for (NSInteger i=0; i<[answer length]; i++) {
                        NSRange range = NSMakeRange(i, 1);//AC
                        NSString *subString = [answer substringWithRange:range];
                        if ([subString isEqualToString:selectionStr]) {
                            [cell.xinghao_btn setSelected:YES];
                        }
                    }
                }
            }
        }
            break;
            
        default:
            break;
    }
    return cell.height;
}
-(CGFloat)heightForCell:(UITableView *)tableView  indexPath:(NSIndexPath *)indexPath{
    HBTableViewCell *cell = (HBTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    NSString *html = [self.dataArray objectAtIndex:indexPath.row];
    NSString *selectionStr = [[html componentsSeparatedByString:@"."] objectAtIndex:0];
    switch (_tableType) {
        case HBListTableViewTypeTest:
        case HBListTableViewTypeResultLookNoAnswer:
        case HBListTableViewTypeResultLook:
        {
            if (cell.isNeedB) {
                if ([selectionStr isEqualToString:myAnswer]) {
                    assignBtn = cell.xinghao_btn;
                    [cell.xinghao_btn setSelected:YES];
                }else{
                    [cell.xinghao_btn setSelected:NO];
                }
            }else{
                if(myAnswer.length>0){
                    for (NSInteger i=0; i<[myAnswer length]; i++) {
                        NSRange range = NSMakeRange(i, 1);//AC
                        NSString *subString = [myAnswer substringWithRange:range];
                        if ([subString isEqualToString:selectionStr]) {
                            [cell.xinghao_btn setSelected:YES];
                        }
                    }
                }
            }
        }
            break;
            
        default:
            break;
    }
    return cell.height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //#ifdef Test_URLConfig
    NSString *qtype = [dataDict objectForKey:@"qtype"];
    if ([qtype isEqualToString:@"0"]&&(_tableType!=HBListTableViewTypeResultLook)) {//0是简单题
        if ([self.dataArray count]>0&&([self.dataArray count]-1==indexPath.row)) {
           return  [self heightForCell:tableView indexPath:indexPath];
        }else{
            return 280;
        }
    }else if([qtype isEqualToString:@"1"]||[qtype isEqualToString:@"2"]||[qtype isEqualToString:@"3"]){
        return  [self heightForCell:tableView indexPath:indexPath];
    }else{
        return  [self heightSimpleSingleForCell:tableView indexPath:indexPath];
    }
}

#pragma mark - 题目header view 加载完成
- (void)HBTableViewHeaderView:(HBTableViewHeaderView *)headerView webViewDidFinishLoad:(UIWebView *)webView{
    if (headerView.tag == 99) {
        self.tableHeaderView = headerView;
        [headerView.lineImageView setFrame:CGRectMake(10, headerView.frame.size.height-1, headerView.frame.size.width-20, 1)];
    }
    else if (headerView.tag == 100){
        [lock lock];
        if (!_loadedHeaders) {
            _loadedHeaders = [NSMutableDictionary dictionaryWithCapacity:1.0];
        }
        NSString *key = [NSString stringWithFormat:@"header_%ld",(long)headerView.sectionIndex];
        NSString *hKey = [NSString stringWithFormat:@"height_%ld",(long)headerView.sectionIndex];
         [_loadedHeaders setObject:headerView forKey:key];
        [_loadedHeaders setObject:@(webView.bounds.size.height) forKey:hKey];
//        headerView.backgroundColor = [UIColor orangeColor];
        [self reloadData];

        [lock unlock];
    }
}
- (void)HBTableViewFooterView:(HBTableViewFooterView *)footerView webViewDidFinishLoad:(UIWebView *)webView{
    self.tableFooterView = footerView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1
{
    CGPoint offset = scrollView1.contentOffset;
    
    CGRect bounds = scrollView1.bounds;
    
    CGSize size = scrollView1.contentSize;
    
    UIEdgeInsets inset = scrollView1.contentInset;
    
    CGFloat currentOffset = offset.y + bounds.size.height - inset.bottom;
    
    heightoffset = currentOffset;
    
    CGFloat maximumOffset = size.height;
    
    //当currentOffset与maximumOffset的值相等时，说明scrollview已经滑到底部了。也可以根据这两个值的差来让他做点其他的什么事情
    if((maximumOffset - currentOffset)<40.0)
    {
        FMSCHBLog(@"-----我要刷新数据-----");
    }
   contentSize =scrollView1.contentSize.height;
}
@end
